package tw.com.hitevision.whiteboard.android.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import tw.com.hitevision.whiteboard.android.R;

import static java.lang.StrictMath.abs;

public class OptionsRulerMenuView extends ConstraintLayout implements View.OnTouchListener {
    private OptionRulerMenuListener listener;
    private OptionsRulerMenuAdapter adapter;

    private static final String TAG = OptionsRulerMenuView.class.getSimpleName();

    private static final float CLICK_DRAG_TOLERANCE = 10f; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.
    private float downRawX = 0f;
    private float downRawY = 0f;
    private float dX = 0f;
    private float dY = 0f;

    public OptionsRulerMenuView(Context context) {
        super(context);
        init(context);
    }

    public OptionsRulerMenuView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public OptionsRulerMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        inflate(context, R.layout.options_ruler_menu_view_layout, this);
        View back = findViewById(R.id.ruler_back);
        back.setOnClickListener(v -> OptionsRulerMenuView.this.setVisibility(GONE));
        RecyclerView recyclerView = findViewById(R.id.tools_recycler_view);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3,
                context.getResources().getDimensionPixelOffset(R.dimen.option_ruler_menu_child_margin), false));
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new OptionsRulerMenuAdapter(context, name -> {
            if (listener != null) {
                listener.onRulerIconClick(name);
            }
        });
        recyclerView.setAdapter(adapter);
    }

    public void setSelectedIconList(ArrayList<Boolean> selectedIconList) {
        adapter.setSelectedList(selectedIconList);
        adapter.notifyDataSetChanged();
    }

    public void setListener(OptionRulerMenuListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        MarginLayoutParams layoutParams = (MarginLayoutParams) v.getLayoutParams();
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            downRawX = event.getRawX();
            downRawY = event.getRawY();
            dX = v.getX() - downRawX;
            dY = v.getY() - downRawY;
            return true;
        } else if (action == MotionEvent.ACTION_MOVE) {
            int viewWidth = v.getWidth();
            int viewHeight = v.getHeight();
            View viewParent = (View) v.getParent();
            int parentWidth = viewParent.getWidth();
            int parentHeight = viewParent.getHeight();
            float newX = event.getRawX() + dX;
            newX = 0f > newX ? 0f : newX; // Don't allow the FAB past the left hand side of the parent
//            newX = (parentWidth - viewWidth - layoutParams.rightMargin) > newX ? (parentWidth - viewWidth - layoutParams.rightMargin) : newX;// Don't allow the FAB past the right hand side of the parent
            float newY = event.getRawY() + dY;
            newY = 0f > newY ? 0f : newY; // Don't allow the FAB past the top of the parent
//            newY = (parentHeight - viewHeight - layoutParams.bottomMargin) > newY ? (parentHeight - viewHeight - layoutParams.bottomMargin) : newY; // Don't allow the FAB past the bottom of the parent
            v.bringToFront();
            v.animate()
                    .x(newX)
                    .y(newY)
                    .setDuration(0)
                    .start();
            return true;
        } else if (action == MotionEvent.ACTION_UP) {
            float upRawX = event.getRawX();
            float upRawY = event.getRawY();
            float upDX = upRawX - downRawX;
            float upDY = upRawY - downRawY;
            if (abs(upDX) < CLICK_DRAG_TOLERANCE && abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
                return performClick();
            } else {
                return true;
            }
        } else {
            return super.onTouchEvent(event);
        }
    }

    public interface OptionRulerMenuListener {

        void onRulerIconClick(String name);

    }

}
